
package com.smartboot.plus.utils;


import java.sql.Timestamp;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;


public class DateUtil {
	public static final String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss";


	public static String format(Date date, String pattern) {
		return ConcurrentDateFormat.of(pattern).format(date);
	}


	public static String runtime(long startTime) {
		StringBuilder sb = new StringBuilder();
		long runtime = (System.currentTimeMillis() - startTime) / 1000L;
		if (runtime >= 86400L) {
			sb.append(runtime / 86400L).append("天");
			runtime %= 86400L;
		}

		if (runtime >= 3600L) {
			sb.append(runtime / 3600L).append("小时");
			runtime %= 60L;
		}

		if (runtime >= 60L) {
			sb.append(runtime / 60L).append("分");
		}

		sb.append(runtime % 60L).append("秒");
		return sb.toString();
	}

	public static String getRuntime(Timestamp startTime, Timestamp endTime) {
		StringBuilder sb = new StringBuilder();

		// 将 Timestamp 转换为 LocalDateTime
		LocalDateTime start = startTime.toLocalDateTime();
		LocalDateTime end = endTime.toLocalDateTime();

		// 计算时间差
		Duration duration = Duration.between(start, end);

		// 计算天数
		long days = duration.toDays();
		if (days > 0) {
			sb.append(days).append("天");
		}

		// 计算小时
		long hours = duration.toHours() % 24;
		if (hours > 0) {
			sb.append(hours).append("小时");
		}

		// 计算分钟
		long minutes = duration.toMinutes() % 60;
		if (minutes > 0) {
			sb.append(minutes).append("分");
		}

		// 计算秒数
		long seconds = duration.getSeconds() % 60;
		if (seconds > 0 || sb.length() == 0) { // 如果总时间为0，至少显示秒数
			sb.append(seconds).append("秒");
		}

		return sb.toString();
	}

	public static String format(  String message,   Object... arguments) {
		if (message == null) {
			return "";
		} else if (arguments != null && arguments.length != 0) {
			StringBuilder sb = new StringBuilder((int)((double)message.length() * 1.5));
			int cursor = 0;
			int index = 0;

			int start;
			int end;
			for(int argsLength = arguments.length; (start = message.indexOf(123, cursor)) != -1 && (end = message.indexOf(125, start)) != -1 && index < argsLength; ++index) {
				sb.append(message, cursor, start);
				sb.append(arguments[index]);
				cursor = end + 1;
			}

			sb.append(message.substring(cursor));
			return sb.toString();
		} else {
			return message;
		}
	}

}
